define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
], function($,IO,bootbox,bootstrap,upui,up,jui) {
    $(function () {
        load();
        function load(){
            var grid;
            //每列数据列性
            var columns = [
                {
                    title: '轮播图',
                    dataIndex: 'banner_url',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        return "<span><img src='" + data[datarow].banner_url + "' width='100px;' height='80px;'></span>" ;
                    }
                },

                {
                    title: '发布状态',
                    dataIndex: 'status',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var status = '未发布';
                        if(data[datarow].banner_status){
                            status = '已发布';
                        }
                        return "<span>" + status + "</span>";

                    }
                },
                {
                    title: '操作',
                    dataIndex: 'action',
                    dataSource: function (data, datarow, gridobj, current_column) {
                        var action = "<a href='javascript:void(0)'  style='color: deepskyblue;' class='status' act_id='" +data[datarow].banner_id+"' banner_status = '"+data[datarow].banner_status+"' >发布</a>";
                        if(data[datarow].banner_status){
                            action = "<a href='javascript:void(0)'  style='color: orange;' class='status' act_id='"  +data[datarow].banner_id+"' banner_status = '"+data[datarow].banner_status+"'>撤回</a>";
                        }
                        return "<span>" + action + "&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: red;' class='delete' act_id='"+data[datarow].banner_id+"'>删除</a></span>";
                    }
                },
            ];


            //数组初始化

            function dataInit() {
                grid = new Grid("/ct/act/banner_list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

                //初始化
                grid.Init();
            }

            //获取输入框内容
            function get_input_param() {
                var cate_id=$("#info_category option:selected").attr("data-id");
                var param="cate_id=" + cate_id;
                //console.log(category_id);
                return param;
            }

            dataInit();
            //查询
            $("#searchid").bind("click", function () {
                dataInit();
            });
        }
        //model use
        $("#banner_add_btn").on('click',function(){
            $("#myModal").modal('show');
        })
        //model hidden
        $('#myModal').on('hidden.bs.modal', function () {
            $("#upimgs").attr({src:"/images/addimg.png"});
        });
        //banner_add
        $("#banner_add").on('click',function(){
            var banner_url = $("#upimgs").attr("src");
            if(banner_url ==""||banner_url == "/images/addimg.png"){
                bootbox.alert("请上传图片");
            }
            bootbox.confirm("确定要增加该轮播图？",function(result){
                if(result){
                    IO.post('/ct/act/banner_add',{banner_url:banner_url}, function (d) {
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/banner';
                            }
                        })
                    })
                }
            })

        })
        //删除轮播图
        $(document).on("click",".delete",function(){
            var act_id=$(this).attr("act_id");
            if(act_id){
                bootbox.confirm("确定要删除该轮播图？",function(result){
                    if(result) {
                        IO.post("/ct/act/banner_delete",{banner_id:act_id},function(d){
                            bootbox.alert(d.msg,function(){
                                if (d.code == 0) {
                                    location.href = '/banner';
                                }
                            })
                        });
                    }
                });
            }
        });

        //发布轮播图
        $(document).on("click",".status",function(){
            var act_id=$(this).attr("act_id");
            var status = $(this).attr("banner_status");
            var text = "确定要发布该轮播图？";
            var banner_status = "1";
            if(status == "1"){
                text = "确定要撤回该轮播图？";
                banner_status = "0";
            }
            if(act_id){
                bootbox.confirm(text,function(result){
                    if(result) {
                        IO.post("/ct/act/banner_change",{banner_id:act_id,type:banner_status},function(d){
                            bootbox.alert(d.msg,function(){
                                if (d.code == 0) {
                                    location.href = '/banner';
                                }
                            })
                        });
                    }
                });
            }
        });


        /**-------------图片上传----------------*/
        $('#fileupload').fileupload({
            url: '/upload',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            document.getElementById('upimgs').src = '/images/loading.gif';
        }).on('fileuploadprocessalways', function (e, data) {
        }).on('fileuploadprogressall', function (e, data) {
        }).on('fileuploaddone', function (e, data) {
            var alt = $(e.target).attr('alt');
            if (!alt)
                alt = '';
            $("#upimgs" + alt).attr("src", data._response.result.url);
        });


    });
});